#ifndef __GLF_ALLOCATOR_H_INCLUDED__
#define __GLF_ALLOCATOR_H_INCLUDED__

//! Very simple allocator implementation, containers using it can be used across dll boundaries
#include <cstddef>
#include <new>

class DefaultAllocatorBase
{
protected:
	static void* internal_new(size_t cnt)
	{
		return new char[cnt];
	}

	static void internal_delete(void* const ptr)
	{
		delete[] reinterpret_cast<char*>(ptr);
	}
};

template <typename T, typename AllocatorBase = DefaultAllocatorBase>
class Allocator
	: public AllocatorBase
{
public:

	// STL allocator compliance defs

	typedef size_t size_type;
	typedef std::ptrdiff_t difference_type;
	typedef T* pointer;
	typedef const T* const_pointer;
	typedef T& reference;
	typedef const T& const_reference;
	typedef T value_type;

	template <typename T1>
	struct rebind
	{
		typedef Allocator<T1, AllocatorBase> other;
	};

	Allocator()
	{
	}

	template <typename T2>
	Allocator(const Allocator<T2, AllocatorBase>& /*other*/)
	{
	}

	pointer address(reference r) const
	{
		return &r;
	}

	const_pointer address(const_reference r) const
	{
		return &r;
	}

	//! Allocate memory for an array of objects
	T* allocate(size_t cnt)
	{
		return (T*)AllocatorBase::internal_new(cnt* sizeof(T));
	}

	size_type max_size() const
	{
		return size_type(-1) / sizeof(T);
	}

	//! Deallocate memory for an array of objects
	void deallocate(T* ptr, size_type n = 0)
	{
		AllocatorBase::internal_delete(ptr);
	}

	//! Construct an element
	void construct(T* ptr, const T& e)
	{
		new (ptr) T(e);
	}

	//! Synonym for destruct (for STL compliance).
	void destroy(T* ptr)
	{
		ptr->~T();
	}

	// Below operators are required by some STL implementations

	bool operator == (const Allocator& /*rhs*/) const
	{
		return true;
	}

	bool operator != (const Allocator& /*rhs*/) const
	{
		return false;
	}
}; // end class SAllocator<T>

#endif // __GLF_ALLOCATOR_H_INCLUDED__